package com.edu.exam.entity.curriculum.questionbank;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

// External enum imports
import com.edu.exam.enums.curriculum.questionbank.QuestionAttributeType;

/**
 * 试题扩展属性实体类
 * 对应数据库表：question_extend_attributes
 *
 * 管理试题的扩展属性信息，支持动态添加试题的自定义属性。
 * 提供灵活的试题属性管理能力，满足不同业务场景的扩展需求。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("试题扩展属性表")
@SQLDelete(sql = "UPDATE question_extend_attributes SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class QuestionExtendAttribute {

    /**
     * 扩展属性唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 试题ID
     */
    @NotNull(message = "试题ID不能为空")
    @Column(name = "question_id", nullable = false)
    private Long questionId;

    /**
     * 属性名称
     */
    @NotBlank(message = "属性名称不能为空")
    @Size(max = 50, message = "属性名称长度不能超过50个字符")
    @Column(name = "attribute_name", nullable = false, length = 50)
    private String attributeName;

    /**
     * 属性值
     */
    @Column(name = "attribute_value", columnDefinition = "TEXT")
    private String attributeValue;

    /**
     * 属性类型
     */
    @NotNull(message = "属性类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "attribute_type", nullable = false, length = 20)
    private QuestionAttributeType attributeType;

    /**
     * 属性描述
     */
    @Size(max = 200, message = "属性描述长度不能超过200个字符")
    @Column(name = "attribute_description", length = 200)
    private String attributeDescription;

    /**
     * 是否必需
     */
    @Column(name = "is_required")
    @Builder.Default
    private Boolean isRequired = false;

    /**
     * 是否系统属性
     */
    @Column(name = "is_system")
    @Builder.Default
    private Boolean isSystem = false;

    /**
     * 验证规则
     */
    @Size(max = 500, message = "验证规则长度不能超过500个字符")
    @Column(name = "validation_rule", length = 500)
    private String validationRule;

    /**
     * 默认值
     */
    @Size(max = 200, message = "默认值长度不能超过200个字符")
    @Column(name = "default_value", length = 200)
    private String defaultValue;

    /**
     * 显示顺序
     */
    @Column(name = "display_order")
    @Builder.Default
    private Integer displayOrder = 0;

    /**
     * 属性分组
     */
    @Size(max = 50, message = "属性分组长度不能超过50个字符")
    @Column(name = "attribute_group", length = 50)
    private String attributeGroup;

    /**
     * 属性元数据，JSON格式
     */
    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    /**
     * 记录创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 记录最后更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 最后更新人用户ID
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }

    }